﻿//============================================================
//write by bxl at 2021-06-06 14:04:28
//============================================================



using System.Collections.Generic;
using System;
using BCService.Utils;
using BCCommon;
using BCData.TMS.VehicleTransportFaqCategory;
using BCDto.TMS.VehicleTransportFaqCategory;
using BCEntity.TMS.VehicleTransportFaqCategory;
using BCDto.Sim.Admin.Admin;
using BCEntity.Common.EntityNameConst;
using System.Linq;

namespace BCService.TMS.VehicleTransportFaqCategory
{
    public class VehicleTransportFaqCategoryService : IVehicleTransportFaqCategoryService
    {
        #region var
        private readonly IDatabaseContext databaseContext;
        private IVehicleTransportFaqCategoryData vehicleTransportFaqCategoryData;
        private readonly IRedisService redisService;

        private readonly string key = string.Format("{0}", EntityNameConst.VehicleTransportFaqCategoryEntity);


        #endregion

        #region 构造函数
        public VehicleTransportFaqCategoryService(IRedisService redisService, IVehicleTransportFaqCategoryData vehicleTransportFaqCategoryData, IDatabaseContext databaseContext)
        {
            this.redisService = redisService;
            this.vehicleTransportFaqCategoryData = vehicleTransportFaqCategoryData;
            this.databaseContext = databaseContext;
        }
        #endregion

        #region Add
        /// <summary>
        /// 添加新车辆运输常见问题分类纪录
        /// </summary>
        /// <param name="vehicleTransportFaqCategoryRequestDto"></param>
        /// <param name="admin"></param>
        /// <returns></returns>
        public VehicleTransportFaqCategoryDto Add(VehicleTransportFaqCategoryRequestDto vehicleTransportFaqCategoryRequestDto, AdminDto admin)
        {

            var org = vehicleTransportFaqCategoryData.GetByFaqCategoryAsync(vehicleTransportFaqCategoryRequestDto.FaqCategory).GetAwaiter().GetResult();
            if (org != null)
                throw new ArgumentException("车辆运输常见问题分类纪录 已存在");

            var entity = new VehicleTransportFaqCategoryEntity();
            entity.FaqCategory = vehicleTransportFaqCategoryRequestDto.FaqCategory;
            entity.CreateAdminId = admin.AdminId;
            entity.CreateAdminName = admin.FullName;
            entity.CreateTime = DateTime.Now;
            entity.EditAdminId = admin.AdminId;
            entity.EditAdminName = admin.FullName;
            entity.UpdateTime = DateTime.Now;

            return vehicleTransportFaqCategoryData.InsertAsync(entity).GetAwaiter().GetResult().As<VehicleTransportFaqCategoryDto>();
             
        }
        #endregion

        #region update
        /// <summary>
        /// 修改车辆运输常见问题分类记录
        /// </summary>
        /// <param name="admin"></param>
        /// <param name="vehicleTransportFaqCategoryDto"></param>
        /// <returns></returns>
        public VehicleTransportFaqCategoryDto Update(VehicleTransportFaqCategoryDto vehicleTransportFaqCategoryDto, AdminDto admin)
        {
            if (string.IsNullOrWhiteSpace(vehicleTransportFaqCategoryDto.FaqCategory))
                throw new ArgumentNullException("FaqCategory");

            var entity = vehicleTransportFaqCategoryData.GetEntityAsync(vehicleTransportFaqCategoryDto.VehicleTransportFaqCategoryId).GetAwaiter().GetResult() ?? throw new ArgumentException("要修改的记录不存在!");


            var entityFaqCategory = vehicleTransportFaqCategoryData.GetByFaqCategoryAsync(vehicleTransportFaqCategoryDto.FaqCategory).GetAwaiter().GetResult();
            if (entityFaqCategory != null && entityFaqCategory.VehicleTransportFaqCategoryId != vehicleTransportFaqCategoryDto.VehicleTransportFaqCategoryId)
                throw new ArgumentException("车辆运输常见问题分类纪录 已存在");


            entity.FaqCategory = vehicleTransportFaqCategoryDto.FaqCategory;

            entity.EditAdminId = admin.AdminId;
            entity.EditAdminName = admin.FullName;
            entity.UpdateTime = DateTime.Now;

            this.redisService.DeleteMultipleKey(key, false);
            return vehicleTransportFaqCategoryData.UpdateAsync(entity).GetAwaiter().GetResult().As<VehicleTransportFaqCategoryDto>();

        }
        #endregion

        #region Delete
        /// <summary>
        /// 删除车辆运输常见问题分类记录
        /// </summary>
        /// <param name="vehicleTransportFaqCategoryId"></param>
        /// <returns></returns>
        public void Delete(long vehicleTransportFaqCategoryId)
        {
            this.redisService.DeleteMultipleKey(key, false);
            vehicleTransportFaqCategoryData.DeleteAsync(vehicleTransportFaqCategoryId).GetAwaiter().GetResult();

        }
        #endregion

        #region GetByVehicleTransportFaqCategoryId
        /// <summary>
        /// 获取车辆运输常见问题分类记录
        /// </summary>
        /// <param name="vehicleTransportFaqCategoryId"></param>
        /// <returns></returns>
        public VehicleTransportFaqCategoryDto GetById(long vehicleTransportFaqCategoryId)
        {
            string cacheKey = this.redisService.GetKey(key, vehicleTransportFaqCategoryId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var a = vehicleTransportFaqCategoryData.GetEntityAsync(vehicleTransportFaqCategoryId).GetAwaiter().GetResult();
                return a.As<VehicleTransportFaqCategoryDto>();
            }
            , TimeSpan.FromDays(1));
            return result;
        }
        #endregion


        #region GetList  获取车辆运输常见问题分类记录--[分页]
        public PageableList<VehicleTransportFaqCategoryDto> GetList(string faqCategory, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, faqCategory,  pageIndex, pageSize);

            var result = this.redisService.TryGet(cacheKey, () =>
            {
                int fCount = 0;
                IEnumerable<VehicleTransportFaqCategoryEntity> fList;

                (fList, fCount) = vehicleTransportFaqCategoryData.GetListAsync(faqCategory, pageIndex, pageSize).GetAwaiter().GetResult();

                return new PageableList<VehicleTransportFaqCategoryDto>()
                {
                    Count = fCount,
                    Items = fList.As<IEnumerable<VehicleTransportFaqCategoryDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now,
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion



        #region GetList  获取车辆运输常见问题分类记录--[分页]
        public List<VehicleTransportFaqCategoryDto> GetList(string faqCategory)
        {
            string cacheKey = this.redisService.GetKey(key, faqCategory);

            var result = this.redisService.TryGet(cacheKey, () =>
            { 
                return vehicleTransportFaqCategoryData.GetListAsync(faqCategory).GetAwaiter().GetResult().As<IEnumerable<VehicleTransportFaqCategoryDto>>().ToList(); 
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion
    }
}